package com.maxafa.transparents;


import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainSurfaceView extends SurfaceView  implements SurfaceHolder.Callback, Runnable{

	private boolean m_is_running;
	private SurfaceHolder m_surfaceholder;
	private static int s_screen_width, s_screen_height;
	private Paint m_paint;
	private Thread m_mainthread;
	private Canvas m_lockcanvas;
	private float m_touch_x, m_touch_y;
	//read resource in asset folder
	private Bitmap m_bitmap_lauchericon;
	private AssetManager m_assetmanager;
	private InputStream m_inputStream;
	
	
	public MainSurfaceView(Context context) {
		super(context);
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);		
		setFocusable(true);
		s_screen_width = context.getResources().getDisplayMetrics().widthPixels;
		s_screen_height = context.getResources().getDisplayMetrics().heightPixels;
		m_assetmanager = context.getAssets();
	}

	private void Initialize(){
		m_paint = new Paint();		
		try{
			m_inputStream = m_assetmanager.open("images/ic_launcher_web.png");
			m_bitmap_lauchericon = BitmapFactory.decodeStream(m_inputStream);			
		}catch(Exception e){}
		m_touch_x = s_screen_width/2 + m_bitmap_lauchericon.getWidth();
		m_touch_y = s_screen_width/2;
		m_mainthread = new Thread(this);
		m_mainthread.start();
		m_is_running = true;
		
		
	}
	
	public void GamePause(){
		
	}
	
	public void GameResume(){
		
	}
	
	public void GameExit(){
		
	}
	
	private void GameUpdate(){
		
	}
	
	private void GameDraw(Canvas canvas){
		
		canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);//use to add color
		m_paint.setColor(Color.RED);
		canvas.drawRect(s_screen_width/4 - s_screen_width/8, s_screen_width/4 - s_screen_width/8, s_screen_width/4 + s_screen_width/8, s_screen_width/4 + s_screen_width/8, m_paint);
		canvas.drawBitmap(m_bitmap_lauchericon, s_screen_width/2 + s_screen_width/8, s_screen_width/4 - s_screen_width/8, m_paint);
		//draw alpha blue rect
		m_paint.setColor(Color.BLUE);
		m_paint.setAlpha(100);
		canvas.drawRect(s_screen_width/4, s_screen_width/4, s_screen_width/4 + (s_screen_width/8)*2, s_screen_width/4 + (s_screen_width/8)*2, m_paint);
		//draw alpha bugdroid
		canvas.drawBitmap(m_bitmap_lauchericon, m_touch_x - m_bitmap_lauchericon.getWidth(), m_touch_y - m_bitmap_lauchericon.getHeight(), m_paint);
		m_paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));//use to add color
		m_paint.setAntiAlias(true);//for anti alias circle
		m_paint.setARGB(0xFF, 255, 0, 0);
		canvas.drawCircle(s_screen_width/2, s_screen_height/2, s_screen_width/4, m_paint);
		m_paint.setARGB(0xFF, 0, 255, 0);
		canvas.drawCircle(s_screen_width/2 - s_screen_width/8, s_screen_height/2 + s_screen_width/4, s_screen_width/4, m_paint);
		m_paint.setARGB(0xFF, 0, 0, 255);
		canvas.drawCircle(s_screen_width/2 + s_screen_width/8, s_screen_height/2 + s_screen_width/4, s_screen_width/4, m_paint);		
		canvas.drawCircle(m_touch_x - s_screen_width/8, m_touch_y + s_screen_width/8, s_screen_width/8, m_paint);//draw move circle
	}

	@Override
	public void run() {
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		m_touch_x = e.getX();
		m_touch_y = e.getY();
		return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);		
	}
}
